package com.example.car.mapper;

import com.example.car.entity.*;
import com.example.car.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Component
public class MemberMapper {

    @Autowired
    private MongoTemplate mongoTemplate;

    public List<User> getCustomerList(Criteria criteria, Integer page, Integer limit) {
        if(page == null) {
            page = 1;
        }
        if(limit == null) {
            limit = 10;
        }
        List<User> list = mongoTemplate.find(new Query(criteria).skip(page - 1).limit(limit), User.class);
        return list;
    }


    public List<Driver> getDriverList(Integer page, Integer limit) {
        List<Driver> list = mongoTemplate.find(new Query().skip(page - 1).limit(limit), Driver.class);
        return list;
    }

    public User login(User user) {
        User users = mongoTemplate.findOne(Query.query(Criteria.where("account").is(user.getAccount()).and("password").is(user.getPassword())), User.class);
        return users;
    }

    public Role getUserRoleByUserId(String userId) {

        List<UserRole> list = mongoTemplate.find(new Query(Criteria.where("user_id").is(userId)), UserRole.class);

        List<String> collect = list.stream().map(item -> {
            return item.getRoleId();
        }).collect(Collectors.toList());

        List<Role> roleList = mongoTemplate.find(new Query(Criteria.where("_id").in(collect)), Role.class);
        if (roleList == null || roleList.size() == 0) {
            return null;
        }
        Role role = roleList.get(0);
        for (Role item : roleList) {
            if ("管理员".equals(item.getName())) {
                role = item;
            }
        }
        return role;
    }

    public String getRoleId(String role) {
        Role item = mongoTemplate.findOne(new Query(Criteria.where("name").is(role)), Role.class);
        return item.getId();
    }

    public List<RouterRole> getRouterId(String roleId) {
        List<RouterRole> routerRoles = mongoTemplate.find(new Query(Criteria.where("role_id").is(roleId)), RouterRole.class);
        return routerRoles;
    }

    public List<Router> getRouter(List<String> collect) {
        List<Router> routerList = mongoTemplate.find(new Query(Criteria.where("id").in(collect)), Router.class);
        return routerList;
    }

    public void updateUser(UserVo userVo) {
        User user = mongoTemplate.findOne(new Query(Criteria.where("_id").is(userVo.getId())), User.class);
        if(user == null) {
            // todo 异常
        }
        Update update = new Update();
        update.set("name", userVo.getName());
        update.set("age", userVo.getAge());
        update.set("gender", userVo.getGender());
        update.set("birth", userVo.getBirth());
        update.set("addr", user.getAddr());
        mongoTemplate.updateFirst(Query.query(Criteria.where("_id").is(userVo.getId())), update, User.class);
    }

    public User getUserByUserId(String userId) {
        User user = mongoTemplate.findOne(new Query(Criteria.where("id").is(userId)), User.class);
        return user;
    }

    public String checkPhoneAndAccount(String phone, String account) {
        User user1 = mongoTemplate.findOne(Query.query(Criteria.where("phone").is(phone)), User.class);
        if (user1 != null) {
            return "该电话已被注册";
        }
        User user2 = mongoTemplate.findOne(Query.query(Criteria.where("account").is(account)), User.class);
        if (user2 != null) {
            return "该账号已被注册";
        }
        return null;
    }

    public boolean save(User user) {
        String id = UUID.randomUUID().toString();
        user.setId(id);
        User save = mongoTemplate.save(user);
        if (save == null) {
            return false;
        }
        UserRole userRole = new UserRole();
        userRole.setUserId(id);
        userRole.setRoleId("624e75e5d45d00005b002e03");
        mongoTemplate.save(userRole);
        return true;
    }

    public void deleteUserById(String userId) {
        mongoTemplate.remove(new Query(Criteria.where("_id").is(userId)), User.class);
        mongoTemplate.remove(new Query(Criteria.where("user_id").is(userId)), UserRole.class);
    }
}

